package variabilitysmelldetection.measurement.measures.component;

import java.util.List;

import org.eclipse.jdt.core.dom.ASTParser;

import variabilitysmelldetection.measurement.astvisitors.CouplingBetweenComponentsVisitor;
import variabilitysmelldetection.measurement.measures.Measure;
import variabilitysmelldetection.measurement.models.ComponentItem;


/**
 * A is coupled to B if:
 * 
 * A has an attribute that refers to (is of type) B.
 * A calls on services of an object B.
 * A has a method that references B (via return type or parameter).
 * A is a subclass of (or implements) class B.
 * 
 */
public class CouplingBetweenObjects extends Measure {

	public CouplingBetweenObjects() {
		super("Coupling Between Object Classes","CBO");
	}

	@Override
	public void measure(List<ComponentItem> items) {
		for (ComponentItem classe : items) {
			measure(classe);
		}
	}	
	
	private void measure(ComponentItem componentItem) {
		CouplingBetweenComponentsVisitor mcb = new CouplingBetweenComponentsVisitor(componentItem);
		componentItem.getCompilationUnit(ASTParser.K_COMPILATION_UNIT).accept(mcb);
		
		/*
		 // DEBUG
		 StringBuilder sb = new StringBuilder();
		for (String s : mcb.getReferencedTypes()) {
			sb.append(s);
			sb.append(", ");
		}
		System.out.println(classItem.getParentName()+"/"+classItem.getName()+": "+sb.toString());*/
		
		CouplingBetweenObjects cbo = new CouplingBetweenObjects();
		cbo.setValue(mcb.getCBO());
		componentItem.addMeasure(cbo);
	}
}
